<?php

/**
 * This is the model class for table "sys_user".
 *
 * The followings are the available columns in table 'sys_user':
 * @property integer $id_user
 * @property integer $id_kary
 * @property string $username
 * @property string $password
 *
 * The followings are the available model relations:
 * @property Karyawan $idKary
 */
class SysUser extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return SysUser the static model class
	 */
        public $nama;
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'sys_user';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('id_kary, username, password', 'required'),
			array('id_kary', 'numerical', 'integerOnly'=>true),
			array('username, password', 'length', 'max'=>50),
                        array('nama', 'safe', 'on'=>'search'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id_user, id_kary, username, password', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'idKary' => array(self::BELONGS_TO, 'Karyawan', 'id_kary'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id_user' => 'Id User',
			'id_kary' => 'Id Kary',
			'username' => 'Username',
			'password' => 'Password',
                        'nama' => 'Karyawan',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
        public function validatePassword($password) {  
		return md5($password) === $this->password;  
	}
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.
                
		$criteria=new CDbCriteria;
                $criteria->select = 't.*,k.nama'; 
                $criteria->join = ' inner join karyawan k on k.id_kary = t.id_kary';
		$criteria->compare('id_user',$this->id_user);
		$criteria->compare('id_kary',$this->id_kary);
		$criteria->compare('username',$this->username,true);
		$criteria->compare('password',$this->password,true);
                $criteria->compare('nama',$this->nama,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
                        'sort'=>array(
                                    'attributes'=>array(
                                                    'username'=>array(
                                                            'asc'=>'username',
                                                            'desc'=>'username DESC',
                                                    ),
                                                    'nama'=>array(
                                                            'asc'=>'nama',
                                                            'desc'=>'nama DESC',
                                                    ),
                                    )
                            ),
		));
	}
}